package com.example.xncs;


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class AppSelectionActivity extends AppCompatActivity {

    private RecyclerView recyclerViewApps;
    private EditText etSearch;
    private Button btnSearch, btnCancel;
    private List<MainActivity.AppInfo> appList;
    private List<MainActivity.AppInfo> filteredAppList;
    private AppSelectionAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_selection);

        initViews();
        loadAppList();
        setupAdapter();
        setupSearch();
    }

    private void initViews() {
        recyclerViewApps = findViewById(R.id.recyclerViewApps);
        etSearch = findViewById(R.id.etSearch);
        btnSearch = findViewById(R.id.btnSearch);
        btnCancel = findViewById(R.id.btnCancel);

        recyclerViewApps.setLayoutManager(new LinearLayoutManager(this));

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void loadAppList() {
        appList = getIntent().getParcelableArrayListExtra("APP_LIST");
        if (appList == null) {
            appList = new ArrayList<>();
        }
        filteredAppList = new ArrayList<>(appList);
    }

    private void setupAdapter() {
        adapter = new AppSelectionAdapter(this, filteredAppList);
        recyclerViewApps.setAdapter(adapter);

        adapter.setOnAppClickListener(new AppSelectionAdapter.OnAppClickListener() {
            @Override
            public void onAppClick(MainActivity.AppInfo appInfo) {
                returnSelectedApp(appInfo);
            }
        });
    }

    private void setupSearch() {
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                filterApps(etSearch.getText().toString());
            }
        });

        // 实时搜索
        /*
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                filterApps(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
        */
    }

    private void filterApps(String query) {
        filteredAppList.clear();

        if (query.isEmpty()) {
            filteredAppList.addAll(appList);
        } else {
            String lowerQuery = query.toLowerCase();
            for (MainActivity.AppInfo app : appList) {
                if (app.getAppName().toLowerCase().contains(lowerQuery) ||
                        app.getPackageName().toLowerCase().contains(lowerQuery)) {
                    filteredAppList.add(app);
                }
            }
        }

        adapter.notifyDataSetChanged();
    }

    private void returnSelectedApp(MainActivity.AppInfo appInfo) {
        Intent resultIntent = new Intent();
        resultIntent.putExtra("SELECTED_APP", appInfo);
        setResult(RESULT_OK, resultIntent);
        finish();
    }
}